# -*- coding:utf-8 -*-
import cv2 as cv
import numpy as np
import requests
from PIL import Image
import pytesseract #要配置tesseract-ocr 引擎的
#图片等比例扩大
def edit_resize(img,i):
    imgInfo = img.shape
    print(imgInfo)  # 输出的三个数据为高度，宽度，模式，模式为3即一个像素点由三种颜色组成
    height = imgInfo[0]
    width = imgInfo[1]
    mode = imgInfo[2]
    # 1 放大 缩小2 等比例 非等比例
    dstHeight = int(height * i)  # 高度变为0.2倍
    dstWidth = int(width * i)  # 宽度变为0.2倍
    img = cv.resize(img, (dstWidth, dstHeight))  # 双线性插值（下次介绍）
    return img

#灰度处理
def edit_HuiDU(img):
    img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)  # 灰度处理
    cv.imshow('HuiDU_img', img)
    return img
#二值化
def edit_2Zhi(img):
    #图像的二值化就是将图像上的像素点的灰度值设置为0或255，这样将使整个图像呈现出明显的黑白效果
    # ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    # cv2.threshold(源图片, 阈值, 填充色, 阈值类型)
    #阈值0 小于阈值的像素点置0	大于阈值的像素点置填充色
    ret, img = cv.threshold(img, 10, 255, cv.THRESH_OTSU)
    cv.imshow('edit_2Zhi', img)
    return img
# 去除线
def edit_DeleteLine(img):

    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))#去除线
    img = cv.morphologyEx(img, cv.MORPH_OPEN, kernel)
    cv.imshow('edit_DeleteLine', img)
    # img = Image.fromarray(img)
    return img
# 膨胀
def edit_PengZhang(img):
    kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))
    img = cv.morphologyEx(img, cv.MORPH_OPEN, kernel)
    # img = Image.fromarray(img)
    cv.imshow('edit_PengZhang', img)
    return img
def edit_FuShi(img):
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (1,3)) # 腐蚀矩
    img = cv.dilate(img, kernel, iterations=0)
    cv.imshow('edit_PengZhang1', img)
    # 图像腐蚀

    # # 图像膨胀
    # kernel = np.ones((1,1), np.uint8)
    # img = cv.dilate(img, kernel, iterations=1)
    # cv.imshow('edit_PengZhang', img)
    return img
# 黑色背景变为白色背景
def edit_HeiBaiChange(img):
    cv.bitwise_not(img, img)# 黑色背景变为白色背景
    cv.imshow('HeiBaiChange', img)
    return img
# 图片转换成字符串
def getString(img):
    text = pytesseract.image_to_string(img)
    print("This OK:%s" % text)
def download(num):
    url = 'http://m.wenzhaihui.com/e/ShowKey/?v=reg&t=0.6905331937073094'
    for i in range(200):
        num = int(i)
        img = requests.get(url)
        with open(f'imgs\\{num}.jpg', 'wb') as f:
            f.write(img.content)
        img = cv.imread(f'imgs\\{num}.jpg')  # 读取图片对象
        # img = edit_resize(img,6)
        # img = cv.medianBlur(img, 7)
        img = edit_HuiDU(img)
        # #
        # # # img = edit_FuShi(img)
        # #
        # # img = edit_PengZhang(img)
        # img = edit_2Zhi(img)
        img = edit_HeiBaiChange(img)
        # # img = edit_HeiBaiChange(img)
        cv.imwrite(f'imgs\\{num}.jpg', img)  # 保存图像到:H:\\img


if __name__ == '__main__':

    # img = cv.imread("b.jpg")  #读取图片对象
    # img = edit_resize(img,5)
    # img = edit_HuiDU(img)
    # img = edit_2Zhi(img)
    # img = edit_HeiBaiChange(img)
    # # img = edit_PengZhang(img)
    # img  =edit_FuShi(img)
    # getString(img)
    # cv.waitKey()
    # cv.destroyAllWindows()
    download(200)